using System.Collections.Generic;
using System.Text;

namespace Framework
{
    public static partial class Utils
    {
        public static class Parse
        {
            private static readonly Stack<char> _split = new Stack<char>();
            private static readonly List<string> _listTmp = new List<string>();
            private static readonly StringBuilder _stringBuilder = new StringBuilder();
            public static string ParseString(string value)
            {
                return value;
            }
            public static bool ParseBool(string value)
            {
                if (string.IsNullOrEmpty(value)) return false;
                if (value == "0") return false;
                if (value == "1") return true;
                bool.TryParse(value, out bool v);
                return v;
            }
            public static byte ParseByte(string value)
            {
                if (string.IsNullOrEmpty(value)) return 0;
                byte.TryParse(value, out byte v);
                return v;
            }
            public static int ParseInt(string value)
            {
                if (string.IsNullOrEmpty(value)) return 0;
                int.TryParse(value, out int v);
                return v;
            }
            public static float ParseFloat(string value)
            {
                if (string.IsNullOrEmpty(value)) return 0f;
                float.TryParse(value, out float v);
                return v;
            }
            public static short ParseShort(string value)
            {
                if (string.IsNullOrEmpty(value)) return 0;
                short.TryParse(value, out short v);
                return v;
            }
            public static long ParseLong(string value)
            {
                if (string.IsNullOrEmpty(value)) return 0;
                long.TryParse(value, out long v);
                return v;
            }
            public static string[] ParseList(string value)
            {
                _listTmp.Clear();
                if (string.IsNullOrEmpty(value)) return null;
                _split.Clear();
                _stringBuilder.Clear();
                if (value.IndexOf("[") == 0 || value.IndexOf("{") == 0)
                {
                    value = value.Substring(1);
                }
                if (value.LastIndexOf("]") == value.Length - 1 || value.LastIndexOf("}") == value.Length - 1)
                {
                    value = value.Substring(0, value.Length - 1);
                }
                var len = value.Length;
                for (int i = 0; i < len; i++)
                {
                    var item = value[i];
                    if (item == ',')
                    {
                        if (_split.Count == 0)
                        {
                            _listTmp.Add(_stringBuilder.ToString());
                            _stringBuilder.Clear();
                            continue;
                        }
                    }
                    _stringBuilder.Append(item);
                    if (item == '[')
                    {
                        _split.Push(']');
                    }
                    else if (item == '{')
                    {
                        _split.Push('}');
                    }
                    else if (item == '(')
                    {
                        _split.Push(')');
                    }
                    else if (item == ']' || item == ')' || item == '}')
                    {
                        if (_split.Count > 0)
                        {
                            var l = _split.Peek();
                            if (l == item)
                            {
                                _split.Pop();
                            }
                        }
                    }
                }
                if (_stringBuilder.Length > 0)
                {
                    _listTmp.Add(_stringBuilder.ToString());
                }
                return _listTmp.ToArray();
            }
            
        }
    }
}